package com.ice.ucenter.controller;

import com.ice.api.ucenter.MenuControllerApi;
import com.ice.domain.model.Tree;
import com.ice.domain.model.response.CommonCode;
import com.ice.domain.model.response.QueryResponseResult;
import com.ice.domain.model.response.QueryResult;
import com.ice.domain.ucenter.MenuDO;
import com.ice.framework.web.BaseController;
import com.ice.ucenter.service.MenuService;
import com.ice.utils.Query;
import com.ice.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xia 菜单控制中心
 */
@RestController
@RequestMapping("/ucenter/menu")
public class MenuController extends BaseController implements MenuControllerApi {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    String prefix = "system/menu";
    @Autowired
    MenuService menuService;

    @GetMapping()
    String menu(Model model) {
        return prefix+"/menu";
    }

    @RequestMapping("/list")
    @ResponseBody
    public QueryResponseResult<MenuDO> list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<MenuDO> menus = menuService.list(query);
        int total = menuService.count(query);

        QueryResult<MenuDO> queryResult = new QueryResult<MenuDO>();
        queryResult.setList(menus);
        queryResult.setTotal(total);

        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }

    @GetMapping("/add/{pId}")
    String add(Model model, @PathVariable("pId") Long pId) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
        return prefix + "/add";
    }


    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        MenuDO mdo = menuService.get(id);
        Long pId = mdo.getParentId();
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
        model.addAttribute("menu", mdo);
        return prefix+"/edit";
    }

    @PostMapping("/save")
    @ResponseBody
    R save(MenuDO menu) {

        if (menuService.save(menu) > 0) {
            return R.ok();
        } else {
            return R.error(1, "保存失败");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    R update(MenuDO menu) {

        if (menuService.update(menu) > 0) {
            return R.ok();
        } else {
            return R.error(1, "更新失败");
        }
    }

    @PostMapping("/remove")
    @ResponseBody
    R remove(Long id) {

        if (menuService.remove(id) > 0) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }

    @GetMapping("/tree")
    @ResponseBody
    Tree<MenuDO> tree() {
        Tree<MenuDO>  tree = menuService.getTree();
        return tree;
    }

    @GetMapping("/tree/{roleId}")
    @ResponseBody
    Tree<MenuDO> tree(@PathVariable("roleId") Long roleId) {
        Tree<MenuDO> tree = menuService.getTree(roleId);
        return tree;
    }

    @Override
    @GetMapping("/getTreeMenu")
    @ResponseBody
    public List<Tree<MenuDO>> getTreeMenu() {
        Long id = Long.parseLong(getUserId());
        return menuService.listMenuTree(id);
    }
}
